<?php
namespace frontend\models;

use common\models\User;
use common\WuMing;
use yii\base\Model;
use Yii;

/**
 * Signup form
 */
class UserInfoForm extends Model
{
    public $username;
    public $nickname;
    public $email;
    public $password;
    public $rePassword;
    public $signature;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['nickname', 'string', 'min' => 2, 'max' => 15],

//            ['email', 'filter', 'filter' => 'trim'],
//            ['email', 'required','message'=>'请填写邮箱'],
//            ['email', 'email','message'=>'邮箱格式不正确'],
//            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => '邮箱已存在'],

            ['signature','safe'],

            [['password','rePassword'],'safe'],
            ['rePassword', 'compare', 'compareAttribute'=>'password', 'message'=>'两次输入的密码不同'],


        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function updateInfo()
    {
        if ($this->validate()) {
            $user = User::findOne(Yii::$app->user->id);
            $user->nickname = $this->nickname;
            $user->signature = $this->signature;
            if($this->password != ""){
                $user->setPassword($this->password);
            }
            if ($user->save()) {
           //     WuMing::dd($user);
                return $user;
            }
        }

        return null;
    }
}
